package com.service.impl;

import com.POJO.Class;
import com.POJO.TeacherSubjectClass;
import com.mapper.ClassMapper;
import com.mapper.TeacherSubjectClassMapper;
import com.service.TeacherSubjectClassService;
import com.util.MybatisConfig;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TeacherSubjectClassServiceImpl是TeacherSubjectClassService的具体实现类，
 * 提供对教师-科目-班级关系的各种操作，如获取关系、新增关系和删除关系等。
 */
@Service
public class TeacherSubjectClassServiceImpl implements TeacherSubjectClassService {
    private TeacherSubjectClassMapper teacherSubjectClassMapper;
    private ClassMapper classMapper;
    private SqlSession sqlSession;

    // 初始化sqlSession和teacherSubjectClassMapper
    private void openSession(){
        sqlSession = MybatisConfig.getSession();
        teacherSubjectClassMapper = sqlSession.getMapper(TeacherSubjectClassMapper.class);
        classMapper = sqlSession.getMapper(ClassMapper.class);
    }

    /**
     * 根据教师ID获取所教授的科目和班级
     * @param teacherId 教师ID
     * @return 教师-科目-班级关系列表
     */
    @Override
    public List<TeacherSubjectClass> getTeacherSubjectClassByTeacherId(int teacherId) {
        openSession();
        try {
            return teacherSubjectClassMapper.getTeacherSubjectClassByTeacherId(teacherId);
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 新增教师-科目-班级关系
     * @param teacherSubjectClass 教师-科目-班级关系对象
     */
    @Override
    public void insertTeacherSubjectClass(TeacherSubjectClass teacherSubjectClass) {
        openSession();
        try {
            teacherSubjectClassMapper.insertTeacherSubjectClass(teacherSubjectClass);
            sqlSession.commit();
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 删除教师-科目-班级关系
     * @param id 关系ID
     */
    @Override
    public void deleteTeacherSubjectClass(int id) {
        openSession();
        try {
            teacherSubjectClassMapper.deleteTeacherSubjectClass(id);
            sqlSession.commit();
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * @return
     */
    @Override
    public List<Class> getAllClasses() {
        openSession();
        try {
            return classMapper.getAllClasses();
        }catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        sqlSession.close();
    }
    }

    /**
     * @param clazz
     */
    @Override
    public void insertClass(Class clazz) {
        openSession();
        try {
            classMapper.insertClass(clazz);
            sqlSession.commit();
        }catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        sqlSession.close();
    }

    }

    /**
     * @param clazz
     */
    @Override
    public void updateClass(Class clazz) {
        openSession();
        try {
            classMapper.updateClass(clazz);
            sqlSession.commit();
        }catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        sqlSession.close();
    }

    }

    /**
     * @param clazzId
     */
    @Override
    public void deleteClass(int clazzId) {
        openSession();
        try {
            classMapper.deleteClass(clazzId);
            sqlSession.commit();
        }catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        sqlSession.close();
    }

    }
}
